//
//  WXPAuthExamplePopupView.m
//  MexicoLoan
//
//  Created by wxp on 2025/2/28.
//

#import "WXPAuthExamplePopupView.h"

@implementation WXPAuthExamplePopupView

#pragma mark - lazy
- (UIView *)backView
{
    if (_backView == nil) {
        _backView = [UIView  new];
        _backView.backgroundColor = COLOR_WHITE;
        [GlobalMethod setRoundView:_backView color:COLOR_WHITE numRound:16 width:0];
        _backView.width = SCREEN_WIDTH-2*LEFT_MARDIN;
    }
    return  _backView;
}
- (UILabel *)titleLabel{
    if (_titleLabel == nil) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = COLOR_BLACK;
        _titleLabel.fontNum = 18;
        [_titleLabel fitTitle:@"Ejemplo correcto" variable:0];
    }
    return _titleLabel;
}
- (UIButton *)btnClose{
    if (_btnClose == nil) {
        _btnClose = [UIButton buttonWithType:UIButtonTypeCustom];
        WEAKSELF
        [_btnClose setNoRepeatActionWithInterval:1 action:^(UIButton * _Nonnull button) {
            [NSObject clickFeedback];
            [weakSelf btnEnsureClick];
        }];
        [_btnClose setImage:[UIImage imageNamed:@"icon-login-close"] forState:UIControlStateNormal];
        _btnClose.widthHeight = XY(16+2*LEFT_MARDIN, 16+2*LEFT_MARDIN);
    }
    return _btnClose;
}
- (UIImageView *)bigImg{
    if (_bigImg == nil) {
        _bigImg = [UIImageView new];
        _bigImg.image = [UIImage imageNamed:@"icon-KTP-example-big"];
        _bigImg.widthHeight = XY(Ratio(218), Ratio(120));
    }
    return _bigImg;
}
- (UILabel *)contentLabel{
    if (_contentLabel == nil) {
        _contentLabel = [UILabel new];
        _contentLabel.textColor = COLOR_BLUE;
        _contentLabel.fontNum = 11;
        _contentLabel.numberOfLines = 0;
        _contentLabel.lineSpace = 2;
    }
    return _contentLabel;
}
- (UILabel *)secTitleLabel{
    if (_secTitleLabel == nil) {
        _secTitleLabel = [UILabel new];
        _secTitleLabel.textColor = COLOR_BLACK;
        _secTitleLabel.fontNum = 18;
        [_secTitleLabel fitTitle:@"Ejemplo incorrecto" variable:0];
    }
    return _secTitleLabel;
}
- (UIView *)exampleView
{
    if (_exampleView == nil) {
        _exampleView = [UIView  new];
        _exampleView.backgroundColor = COLOR_WHITE;
        _exampleView.width = self.backView.width-2*(20);
        CGFloat interval = 10;
        CGFloat viewWidth = (_exampleView.width-2*interval)/3;
        NSArray * titleAry = @[@"No completo",@"Difumina",@"Sobreexposición"];
        NSArray * imageAry = @[@"icon-KTP-error-example-1",@"icon-KTP-error-example-2",@"icon-KTP-error-example-3"];
        for (int i = 0; i < titleAry.count; i ++) {
            UIImageView * image = [UIImageView new];
            image.image = [UIImage imageNamed:imageAry[i]];
            image.widthHeight = XY(viewWidth, viewWidth/85*58);
            image.leftTop = XY((interval+viewWidth)*i, 0);
            [_exampleView addSubview:image];
            
            UILabel * titleLabel = [UILabel new];
            titleLabel.textColor = COLOR_BLACK;
            titleLabel.fontNum = (10);
            NSString * str = [NSString stringWithFormat:@" %@", titleAry[i]];
            [titleLabel setAttributedText:[str setImageName:@"icon-ktp-error-example" imageRect:CGRectMake(0, -2.5, 12, 12) withIndex:0]];
            [titleLabel sizeToFit];
            titleLabel.centerXTop = XY(image.centerX, image.bottom+Ratio(7));
            [_exampleView addSubview:titleLabel];
            if(i == 0){
                _exampleView.height = titleLabel.bottom+1;
            }
        }
    }
    return  _exampleView;
}
- (UILabel *)threeTitleLabel{
    if (_threeTitleLabel == nil) {
        _threeTitleLabel = [UILabel new];
        _threeTitleLabel.textColor = COLOR_BLACK;
        _threeTitleLabel.font = [UIFont boldSystemFontOfSize:(16)];
        [_threeTitleLabel fitTitle:@"Forma recomienda de sacar fotos" variable:0];
    }
    return _threeTitleLabel;
}
- (UILabel *)descLabel{
    if (_descLabel == nil) {
        _descLabel = [UILabel new];
        _descLabel.textColor = COLOR_BLACK;
        _descLabel.fontNum = (12);
        _descLabel.numberOfLines = 0;
        _descLabel.lineSpace = (2);
        [_descLabel fitTitle:@"1. Procure que el entorno de la foto esté bien iluminado, evite entornos demasiado oscuros o demasiado luminosos.\n2. Coloque el INE en el centro de la foto y pulse el centro de la pantalla para enfocar, pulse el botón Tomar foto." variable:(self.backView.width-2*(20))];
    }
    return _descLabel;
}
- (UIButton *)btnEnsure{
    if (_btnEnsure == nil) {
        _btnEnsure = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnEnsure.widthHeight = XY(Ratio(220), BUTTON_HEIGHT);
        [GlobalMethod setRoundView:_btnEnsure color:[UIColor clearColor] numRound:_btnEnsure.height/2 width:0];
        [_btnEnsure setTitle:@"Lo entiendo" forState:UIControlStateNormal];
        _btnEnsure.backgroundColor = COLOR_PRIMARY;
        _btnEnsure.titleLabel.fontNum = 14;
        [_btnEnsure setTitleColor:COLOR_BLACK forState:UIControlStateNormal];
        WEAKSELF
        [_btnEnsure setNoRepeatActionWithInterval:1 action:^(UIButton * _Nonnull button) {
            [NSObject clickFeedback];
            [weakSelf.btnEnsure addClickAnimate];
            [weakSelf btnEnsureClick];
        }];
    }
    return _btnEnsure;
}
- (UIButton *)btnAlbum{
    if (_btnAlbum == nil) {
        _btnAlbum = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnAlbum.widthHeight = XY(self.backView.width, BUTTON_HEIGHT);
        [_btnAlbum setTitle:@"Seleccionar desde álbum>" forState:UIControlStateNormal];
        _btnAlbum.titleLabel.fontNum = 14;
        [_btnAlbum setTitleColor:COLOR_BLUE forState:UIControlStateNormal];
        WEAKSELF
        [_btnAlbum setNoRepeatActionWithInterval:1 action:^(UIButton * _Nonnull button) {
            [weakSelf removeFromSuperview];
            if(weakSelf.blockAlbum){
                weakSelf.blockAlbum();
            }
        }];
    }
    return _btnAlbum;
}


#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.widthHeight = XY(SCREEN_WIDTH, SCREEN_HEIGHT);
        self.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.4];
        [self addSubView];//添加子视图
    }
    return self;
}

//添加subview
- (void)addSubView{
    [self addSubview:self.backView];
    [self.backView addSubview:self.titleLabel];
    [self.backView addSubview:self.btnClose];
    [self.backView addSubview:self.bigImg];
    [self.backView addSubview:self.contentLabel];
    
    [self.backView addSubview:self.secTitleLabel];
    [self.backView addSubview:self.exampleView];
    [self.backView addSubview:self.threeTitleLabel];
    [self.backView addSubview:self.descLabel];
    [self.backView addSubview:self.btnEnsure];
    [self.backView addSubview:self.btnAlbum];
    
    //初始化页面
    [self resetViewWithShowAlbumBtn: false];
}

#pragma mark - 点击事件
- (void)btnEnsureClick{
    [self removeFromSuperview];
    if(self.blockEnsure){
        self.blockEnsure();
    }
}

#pragma mark - 刷新view
- (void)resetViewWithShowAlbumBtn:(BOOL)isShow{
    self.titleLabel.centerXTop = XY(self.backView.width/2, Ratio(26));
    self.btnClose.rightTop = XY(self.backView.width, 0);
    
    self.bigImg.centerXTop = XY(self.titleLabel.centerX, self.titleLabel.bottom+Ratio(15));
    
    [self.contentLabel fitTitle:@"Tips: La Identificación con foto clara aumentará la posibilidad de la aprobación" variable:self.backView.width-2*(30)];
    self.contentLabel.centerXTop = XY(self.bigImg.centerX, self.bigImg.bottom+Ratio(8));
    
    self.secTitleLabel.centerXTop = XY(self.titleLabel.centerX, self.contentLabel.bottom+Ratio(25));
    
    self.exampleView.centerXTop = XY(self.titleLabel.centerX, self.secTitleLabel.bottom+(15));
    
    self.threeTitleLabel.leftTop = XY(20, self.exampleView.bottom+Ratio(22));
    self.descLabel.leftTop = XY(20, self.threeTitleLabel.bottom+Ratio(10));
    
    self.btnEnsure.centerXTop = XY(self.backView.width/2, self.descLabel.bottom+(24));
    
//    self.btnAlbum.hidden = !isShow;
    self.btnAlbum.hidden = true;
    self.btnAlbum.centerXTop = XY(self.btnEnsure.centerX, self.btnEnsure.bottom+Ratio(6));
    
    self.backView.height = self.btnAlbum.hidden ? (self.btnEnsure.bottom+Ratio(20)) : (self.btnAlbum.bottom+Ratio(12));
    self.backView.centerXCenterY = XY(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
}

@end
